package com.bigknow.appstore.web.console.controller;

import com.bigknow.appstore.web.console.app.SubscribeState;
import com.bigknow.appstore.web.console.app.Subscriber;
import com.bigknow.appstore.web.console.service.ISubscriberService;
import com.bigknow.appstore.web.console.service.impl.SubscriberService;
import com.bigknow.appstore.web.framework.controller.AbstractRestController;
import com.bigknow.appstore.web.framework.util.GlobalConstant;
import com.bigknow.appstore.web.framework.vo.ResultInfo;
import com.bigknow.appstore.web.framework.vo.VoFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import static com.bigknow.appstore.web.framework.util.GlobalConstant.SUBSCRIBER_SUCCESS;

/**
 * Created by zhangwei on 2016/10/25.
 */
@RestController
@RequestMapping(value = "/console/sub")
public class SubscriberController extends AbstractRestController<Subscriber, ISubscriberService> {

    @Autowired
    private SubscriberService subscriberService;

    @Override
    protected String getBasePath() {
        return "/console";
    }

    @Override
    protected String getBaseName() {
        return "sub";
    }

    @RequestMapping()
    public ModelAndView sub(Subscriber sub, Integer pageIndex, Integer pageSize) {
        pageIndex = (pageIndex == null) ? GlobalConstant.DEFAULT_PAGE_INDEX : pageIndex;
        pageSize = (pageSize == null) ? GlobalConstant.DEFAULT_PAGE_SIZE : pageSize;
        ModelAndView modelAndView = super.list(sub, pageIndex, pageSize);
        return modelAndView;
    }

    @RequestMapping("/list")
    public ModelAndView list(Subscriber sub, Integer pageIndex, Integer pageSize) {
        pageIndex = (pageIndex == null) ? GlobalConstant.DEFAULT_PAGE_INDEX : pageIndex;
        pageSize = (pageSize == null) ? GlobalConstant.DEFAULT_PAGE_SIZE : pageSize;
        ModelAndView modelAndView = super.list(sub, pageIndex, pageSize);
        return modelAndView;
    }

    @RequestMapping("/approve")
    public ModelAndView approve(String id) {
        ModelAndView view = new ModelAndView("/app/subApplyForm");
        view.addObject("apply", subscriberService.getSub(id, SubscribeState.APPLYING));
        return view;
    }

    @RequestMapping(value = "/subreply/{type}", method = RequestMethod.POST)
    public ResultInfo subreply(Subscriber sub, @PathVariable String type) {
        if (StringUtils.equals(type, "allow")) {
            sub.setState(SubscribeState.SUCCESS);
        } else {
            sub.setState(SubscribeState.FAILED);
        }
        subscriberService.reply(sub);
        return VoFactory.newResultInfo(ResultInfo.SUCCESS, springUtils.getMessage(SUBSCRIBER_SUCCESS));
    }
}
